package com.bulletelc.apollopre.widget;

import android.content.Context;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bulletelc.apollopre.R;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * viewpager的指示器
 * Created by Administrator on 2017/3/20 0020.
 */

public class ViewPagerIndicator {
    private Context context;
    /**
     * 指示器圆点的数量
     */
    private int count;
    private LinearLayout llIndicator;

    public ViewPagerIndicator(Context context, int count, LinearLayout llIndicator) {
        this.context = context;
        this.count = count;
        this.llIndicator = llIndicator;
        initIndicator();
    }

    /**
     * 初始化指示器
     */
    private void initIndicator() {
        if (count > 1) {//当viewpager只有一页时,不添加指示器
            for (int i = 0; i <count ; i++) {
                ImageView iv = new ImageView(context);
                LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(10,10);//宽高为10px
                params.setMargins(10,0,10,0);
                iv.setLayoutParams(params);
                AutoUtils.autoSize(iv);
                llIndicator.addView(iv);
            }
        }
    }

    /**
     * 轮播指示器
     */
    public void carouselIndicator(int position) {
        if (count > 1) {
            for (int i = 0; i <count ; i++) {
                ImageView iv = (ImageView) llIndicator.getChildAt(i);
                if (position != i) {
                    iv.setBackgroundResource(R.drawable.ic_indicator_normal);
                } else {
                    iv.setBackgroundResource(R.drawable.ic_indicator_enable);
                }
            }
        }
    }
}
